﻿using AutoMapper;
using BSChuangXu.API.Read.Appicant.Commend.RoleCommend;
using BSChuangXu.Domain.User;
using BSChuangXu.ErrorCode;
using BSChuangXu.Infrastructure.Interface;
using MediatR;

namespace BSChuangXu.API.Read.Appicant.CommendHandel.RoleCommandHandler
{
    /// <summary>
    /// 用户名唯一验证
    /// </summary>
    public class OnenessRoleNameCommandHandler : IRequestHandler<OnenessRoleNameCommand, APIResult<object>>
    {
        private readonly IBaseRepository<RoleModel> userrReppos;
        private readonly IMapper mapper;
        public OnenessRoleNameCommandHandler(IBaseRepository<RoleModel> userrReppos, IMapper mapper)
        {
            this.mapper = mapper;
            this.userrReppos = userrReppos;
        }
        /// <summary>
        /// 用户名唯一验证
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<APIResult<object>> Handle(OnenessRoleNameCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var user = userrReppos.GetAll().Result.Where(x => x.RoleName == request.RoleName).ToList();
            if (user.Count() > 0)
            {
                result.Code = APIEnum.用户名重复;
                result.Msg = APIEnum.用户名重复.ToString();
            }
            else
            {
                result.Code = APIEnum.用户名唯一通过;
                result.Msg = APIEnum.用户名唯一通过.ToString();
            }
            return Task.FromResult(result);
        }
    }
}
